from selenium import  webdriver
from time import *
import json

def get_cookies():
		#实例化谷歌设置选项
	option = webdriver.ChromeOptions()
	#添加保持登录的数据路径：安装目录一般在C:\Users\Administrator\AppData\Local\Google\Chrome\User Data
	option.add_argument(r"user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data")

	#初始化driver
	driver = webdriver.Chrome(options=option)
	driver.get("https://www.ztestin.com/hall")
	# sleep(3)
	# driver.refresh()
	driver.maximize_window()
	sleep(3)
	get_cookie = driver.get_cookies()
	with open("cookies.txt",'w') as f:
		json.dump(get_cookie,f)
		print("获取cookies。。。ok。。。")
	sleep(3)
	driver.quit()	


# #读取cookie，签到。。。
def read_cookies():

    driver = webdriver.Chrome()

    driver.get("https://www.ztestin.com/hall")
    driver.maximize_window()
    sleep(2)
    with open("cookies.txt", "r") as fp:
        cookies = json.load(fp)
    for cookie in cookies:
        # cookie.pop('domain')
        if 'expiry' in cookie.keys():
            # cookie.pop('expiry')  # 如果报expiry无效的错误  expiry
            del cookie['expiry']
        # if 'domain' in cookie.keys():
        #     cookie.pop('domain')  # 如果报domain无效的错误   
        driver.add_cookie(cookie)
    sleep(1)
    driver.refresh()
    sign_link = driver.find_element_by_xpath('//a[@class="list_grow_a"]')
    sign_link.click()
    sign_in = driver.find_element_by_xpath('//p[text()="签到"]')
    sign_in.click()

get_cookies()
read_cookies()
sleep(10)